<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Login extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/users
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
      	$this->load->model('Users_model', 'user');
      	if( $this->user->hasSession() )
      	{
      		if( $this->user->first_time() )
      			redirect('/login/register', 'refresh');
      		else
      			redirect('/user', 'refresh');
      	}else{
      		$data = array('title' => 'FAT Application | Log in');
	       	$this->load->view('default/header', $data);
			$this->load->view('default/login/login');
			$this->load->view('default/footer');
      	}
	}
	
	public function logout()
	{
       	$this->load->model('Users_model', 'user');
       	if($this->user->logout() )	redirect('/login', 'refresh');
	}
	
	public function login_with_google()
	{
		$this->load->model('Users_model', 'user');
      	if( $this->user->login_with_google() ){
      		if( $this->user->first_time() )
      			redirect('/login/register', 'refresh');
      		else
      			redirect('/user', 'refresh');
      	}else
      		redirect('/login/logout', 'refresh');
  		

	}
	
	public function register()
	{
		
		$this->load->model('Users_model', 'user');
      	if( !$this->user->hasSession() ) redirect('/login', 'refresh');
      	if( !$this->user->first_time() ) redirect('/user', 'refresh');

		$firstname = $this->input->post('firstname');
		if(!$firstname){
			$firstdata = $this->user->first_data();
		 
			$data = array('firstData' => $firstdata, 'title' => 'FAT Application | Registration');
			$this->load->view('default/header', $data);
			$this->load->view('default/login/registration', $data);
			$this->load->view('default/footer');
		}else{
			$lastname = 			$this->input->post('lastname');
			$newornot = 			$this->input->post('new-or-not');
			$email = 				$this->input->post('email');
			$role = 				$this->input->post('role');
			$associationID = 		$this->input->post('associationslist');
			$associationrole = 		$this->input->post('associationrole');
			$associationname = 		$this->input->post('association-name');
			$associationaddress = 	$this->input->post('association-address');
			$associationemail = 	$this->input->post('association-email');
			
			if($role ==="user"){
				$data = array( 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email);
				$userID = $this->user->insert_user($data);
				redirect('/user', 'refresh');
				return;
			}
				
				
			if($newornot === "new"){
				$this->load->model('Associations_model', 'association');
				$data = array( 'raison_social' => $associationname, 
        				'sigle' => "",  
        				'adresse' => $associationaddress,  
        				'telephone' => "",
        				'mail' => $associationemail,
        				'fondateur' => "",
        				'date_fondation' => "",
        				'contact' => "",
        				'description' => ""
        				);
				$associationID = $this->association->insert($data);
				$data = array( 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email);
				$userID = $this->user->insert($data);
				
				$this->association->add_user($associationID, $userID, $associationrole);
				
			}else{
				$this->load->model('Associations_model', 'association');
				$data = array( 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email);
				$userID = $this->user->insert($data);
				if(strlen($associationID) > 0)
					$this->association->add_user($associationID, $userID, $associationrole);
			}
			
			redirect('/user', 'refresh');
		}
		
       	
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/users.php */